import myMoment from "@/utils/common/myMoment";

export const getStartEndTimeString = (time: any) => {
    let start = time.split(',')[0]
    let end = time.split(',')[1]
    return myMoment(start).format('MMMDo HH:mm') + '-' + myMoment(end).format('HH:mm')
}

export const getStartTimeString = (time: any) => {
    return myMoment(time).format("MMMDo HH:mm")
}
export const getEndTimeString = (time: any) => {
    return myMoment(time).format("HH:mm")
}

/**
 * 是否为链接
 * @param url
 */
export function checkURL(url: any) {
    const Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
    const objExp = new RegExp(Expression);
    if (objExp.test(url)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 防抖 一定时间内执行一次  点击后时间到才触发，再次点击重新计时
 * 单位时间内事件触发会被重置，避免事件被误伤触发多次
 * @param fn
 * @param delay
 */
export function debounce(fn: Function, delay: number) {
    let timer: any
    return function () {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            fn()
        }, delay)
    }
}

/**
 * 节流 一段时间内触发一次 触发后经过一段才能再触发
 * 单位时间内事件只能触发一次
 * @param fn
 * @param delay
 */
export function throttle(fn: Function, delay: number) {
    let isThtottle = true
    return () => {
        if (!isThtottle) return
        isThtottle = false
        setTimeout(() => {
            fn()
            isThtottle = true
        }, delay)
    }
}


export function getBMI(height: any, weight: any) {
    return (weight / (height * height) * 10000).toFixed(1)
}
export const getBMIStatus = (value: any) => {
    if (value < 18.5) {
        return "过低"
    } else if (value < 23.9) {
        return "正常"
    } else if (value < 27.9) {
        return "超重"
    } else {
        return "肥胖"
    }
}

export const isEmail=(val:any)=>{
    const rExp : RegExp =  /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/;
    return rExp.test(val)
}

export const uni2px=(val:any)=>{
    return uni.upx2px(val)
}